package com.vancouver.knows.ui.activity.homepage.livefeed;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.pangu.AMApplication;
import com.pangu.bean.PageInfo;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.HomeLiveFeedInfo;
import com.vancouver.knows.business.task.mark.LiveFeedResultTaskMark;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.userpost.UserPostDetailActivity;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.util.CircleTransform;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by llf 2016/6/13.
 */
public class LiveFeedResultListView extends PullListItemBrowser<AMApplication> {

    private BusinessModule bModule;
    private Context mContext;
    private List<HomeLiveFeedInfo> liveFeedInfo;

    public LiveFeedResultListView(Context context, ARefreshViewController mController) {
        super(context, mController);
        mContext = context;
        bModule = ((MainApplication) imContext).getBusinessModule();
       // liveFeedInfo = bModule.getCacheManager().getHomeLiveFeedInfoCache();
    }


    @Override
    protected BaseAdapter createAdapter() {
        return new LiveFeedAdapter();
    }

    @Override
    protected void handleLoadNewItems(ATaskMark taskMark) {
        LiveFeedResultTaskMark liveFeedTaskMark = (LiveFeedResultTaskMark) taskMark;
        PageInfo pageInfo = liveFeedTaskMark.getPageInfo();
        bModule.getServiceWrapper().liveFeed(this, liveFeedTaskMark
                , String.valueOf(pageInfo.getNextFrom()), String.valueOf(pageInfo.getSize()), "live_feed");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HomeLiveFeedInfo liveFeedInfo = (HomeLiveFeedInfo)parent.getAdapter().getItem(position);
        if (liveFeedInfo != null) {
            String postType = liveFeedInfo.getCommentPostType();
            if ("post".equals(postType)) {
                Intent intent = new Intent(mContext, CityNewsDetailActivity.class);
                intent.putExtra("postId", liveFeedInfo.getId());
                mContext.startActivity(intent);
                //  } else if ("event".equals(postType)) {
                //  cla = EventDetailActivity.class;
            } else if ("business".equals(postType)) {
                Intent intent = new Intent(mContext, CityDiscountDetailActivity.class);
                intent.putExtra("postId", liveFeedInfo.getId());
                mContext.startActivity(intent);
            } else if ("userpost".equals(postType)) {
                Intent intent = new Intent(mContext, UserPostDetailActivity.class);
                intent.putExtra(UserPostDetailActivity.POST_ID_EXTRA, liveFeedInfo.getId());
                mContext.startActivity(intent);
            }
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    protected void addListHeader(ListView listView) {
        super.addListHeader(listView);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void setListViewParameters(ListView listView) {
        super.setListViewParameters(listView);
        listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
        listView.setDividerHeight(1);
    }

    public class LiveFeedAdapter extends BaseAdapter {

        private LayoutInflater lf;

        public LiveFeedAdapter() {
            this.lf = LayoutInflater.from(getContext());
        }

        @Override
        public int getCount() {
            return bModule.getCacheManager().getHomeLiveFeedInfoCache().size();
        }

        @Override
        public HomeLiveFeedInfo getItem(int position) {
            return bModule.getCacheManager().getHomeLiveFeedInfoCache().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewValueHand hand = null;
            View view = convertView;
            if (position == getCount() - 1) {
                tryQueryNewItems(true, ATaskMark.TYPE_DEFAULT);
            }
            if (convertView == null) {
                view = lf.inflate(R.layout.all_speak_view, parent, false);
                hand = new ViewValueHand(view);

                view.setTag(hand);
            } else {
                hand = (ViewValueHand) view.getTag();
            }

            HomeLiveFeedInfo info = (HomeLiveFeedInfo) getItem(position);
            setLiveFeedText(info, hand.titleTxt, hand.replyTxt, hand.image);
            return view;
        }

        public class ViewValueHand {
            @Bind(R.id.content_title)
            TextView titleTxt;
            @Bind(R.id.image)
            ImageView image;
            @Bind(R.id.reply)
            TextView replyTxt;

            public ViewValueHand(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    private void setLiveFeedText(HomeLiveFeedInfo listInfo,TextView contentTitle, TextView reply, ImageView imageTitle){

        if (listInfo.getActivityType().equals("post")) {
            contentTitle.setText(getStyleText(listInfo.getCommentPostTitle(),listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.new_post)+" "));
        } else if (listInfo.getActivityType().equals("image")) {
            contentTitle.setText(getStyleText(listInfo.getCommentPostTitle(),listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.new_business_image)+" "));
        } else {
            contentTitle.setText(getStyleText(listInfo.getCommentPostTitle(),listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.reviews_of)+" "));
        }
        reply.setText(listInfo.getCommentContent());
        setImage(listInfo, imageTitle);
    }

    private void setImage(HomeLiveFeedInfo info, ImageView image){
        Picasso.with(mContext).load(info.getCommentAuthorAvatar()).transform(new CircleTransform())//.placeholder(R.drawable.marker25)
                .resizeDimen(R.dimen.homepage_live_feed_width, R.dimen.homepage_live_feed_hight).centerCrop()
                .into(image);
    }

    private Spanned getStyleText(String content, String authorName, String time) {
        return Html.fromHtml("<font color=\"#F05326\">" + authorName + "</font>" + time + "<b>" + content + "</b>");
    }
}
